'''
Author: xialang333@gmail.com
Date: 2025-08-04 14:35:19
LastEditTime: 2025-08-04 16:41:10
'''
from fastapi import APIRouter, Request, Depends, HTTPException
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from sqlalchemy.orm import Session

from app.crud import get_exporters, get_exporter
from app.database import get_db
from app.schemas import Exporter

router = APIRouter()
templates = Jinja2Templates(directory="templates")

@router.get("/", response_class=HTMLResponse)
async def dashboard(request: Request, db: Session = Depends(get_db)):
    exporters = get_exporters(db)
    return templates.TemplateResponse(
        "dashboard.html", 
        {"request": request, "exporters": exporters}
    )

@router.get("/add-exporter", response_class=HTMLResponse)
async def add_exporter_page(request: Request):
    return templates.TemplateResponse(
        "add_exporter.html", 
        {"request": request}
    )

@router.get("/edit-exporter/{exporter_id}", response_class=HTMLResponse)
async def edit_exporter_page(request: Request, exporter_id: int, db: Session = Depends(get_db)):
    exporter = get_exporter(db, exporter_id)
    if not exporter:
        raise HTTPException(status_code=404, detail="Exporter not found")
    return templates.TemplateResponse(
        "edit_exporter.html", 
        {"request": request, "exporter": exporter}
    )